package com.wx.tnsenterprise.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.common.api.CommonResult;
import com.wx.tnsenterprise.mapper.RecruitMapper;
import com.wx.tnsenterprise.model.JobModel;
import com.wx.tnsenterprise.model.dto.FunnelRecDto;
import com.wx.tnsenterprise.model.dto.PageDto;
import com.wx.tnsenterprise.model.po.HrPo;
import com.wx.tnsenterprise.model.po.RecruitPo;
import com.wx.tnsenterprise.model.vo.RecDetailVo;
import com.wx.tnsenterprise.model.vo.RecNavVo;
import com.wx.tnsenterprise.model.vo.RecruitHotVo;
import com.wx.tnsenterprise.model.dto.RecruitDto;
import com.wx.tnsenterprise.service.FeignBaseDataService;
import com.wx.tnsenterprise.service.RecruitService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 林建权
 * @version 1.0
 * @date 2020/7/28 16:54
 */
@Service
public class RecuritServiceImpl implements RecruitService {

    @Resource
    private RecruitMapper recruitMapper;

    @Resource
    private FeignBaseDataService feignBaseDataService;

    @Override
    public List<RecruitPo> findAll() {
        return recruitMapper.findAll();
    }

    @Override
    public PageInfo<RecruitPo> findAllPageInfo(PageDto pageDto) {
        if (pageDto.getPage()==null){
            pageDto.setPage(1);
        }
        if (pageDto.getPageSize()==null){
            pageDto.setPageSize(10);
        }
        PageHelper.startPage(pageDto.getPage(),pageDto.getPageSize());
        List<RecruitPo> list=recruitMapper.findAll();
        PageInfo<RecruitPo> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public RecruitPo findById(Integer id) {
        return recruitMapper.findById(id);
    }

    @Override
    public int insert(RecruitPo recruitPo) {
        return recruitMapper.insert(recruitPo);
    }

    //boss发布招聘
    @Override
    public int insertByRecVo(RecruitDto recruitDto) {
        RecruitPo recruitPo = new RecruitPo();
        BeanUtils.copyProperties(recruitDto, recruitPo);
        String city = recruitDto.getWorkCity().get(2);
        recruitPo.setWorkCity(city);
        return recruitMapper.insert(recruitPo);
    }

    //首页热招
    @Override
    public List<RecruitHotVo> findHotRec(Integer length) {
         List<RecruitPo> recruitPoList = recruitMapper.findHotRec(length);
         List<RecruitHotVo> list = new ArrayList<>();
         for (RecruitPo recruitPo : recruitPoList){
             RecruitHotVo recruitHotVo = new RecruitHotVo();
             BeanUtils.copyProperties(recruitPo,recruitHotVo);
             recruitHotVo.setRecId(recruitPo.getId());
             list.add(recruitHotVo);
         }
        return list;
    }

    //职业栏目搜索招聘
    @Override
    public PageInfo findRecByFunnel(FunnelRecDto funnelRecDto){
        //处理数据转成想要的数据
        RecruitPo recruitPo = handleDto(funnelRecDto);

        PageHelper.startPage(funnelRecDto.getPage(),funnelRecDto.getPageSize());
        List<RecNavVo> list=recruitMapper.findRecByFunnel(recruitPo);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    //职业栏目热门城市
    @Override
    public List<String> findRecHotCity(Integer length) {
        if (length==null){
            length = 10;
        }
        return recruitMapper.findRecHotCity(length);
    }

    //招聘栏目下热门职位
    @Override
    public List<String> findRecHotJob(Integer length) {
        if (length==null){
            length = 10;
        }
        List<Integer> recHotJobId = recruitMapper.findRecHotJob(length);
        List<String> recHotJobName = new ArrayList<>();
        for (Integer id:recHotJobId){
            if (id==null){
                continue;
            }
            CommonResult result = feignBaseDataService.findById(id);
            if (result.getCode()==200){
                JobModel jobModel = JSON.parseObject(JSON.toJSONString(result.getData()), new TypeReference<JobModel>() {});
                recHotJobName.add(jobModel.getJobName());
            }
        }
        return recHotJobName;
    }

    //职位详情页
    @Override
    public RecDetailVo recDetail(Integer id) {
        return recruitMapper.recDetail(id);
    }


    //前端数据简单处理
    private RecruitPo handleDto(FunnelRecDto funnelRecDto){
        //第一行搜索
        if(funnelRecDto.getWorkCity()=="全国"){
            funnelRecDto.setWorkCity(null);
        }
        if(funnelRecDto.getJobAnotherName()=="不限"){
            funnelRecDto.setJobAnotherName(null);
        }

        //第二行搜索
        if (funnelRecDto.getWorkExp()=="不限"){
            funnelRecDto.setWorkExp(null);
        }
        if (funnelRecDto.getWorkDip()=="不限"){
            funnelRecDto.setWorkDip(null);
        }
        if (funnelRecDto.getWorkSalary()=="不限"){
            funnelRecDto.setWorkSalary(null);
        }
        if (funnelRecDto.getEntStageFinace()=="不限"){
            funnelRecDto.setEntStageFinace(null);
        }
        if (funnelRecDto.getEntStageScale()=="不限"){
            funnelRecDto.setEntStageScale(null);
        }

        //分页要求
        if (funnelRecDto.getPage()==null || funnelRecDto.getPage()<=0){
            funnelRecDto.setPage(1);
        }
        if (funnelRecDto.getPageSize()==null || funnelRecDto.getPageSize()<=0){
            funnelRecDto.setPageSize(30);
        }

        //处理时间数据（暂时先不做了）
        RecruitPo recruitPo = new RecruitPo();
        BeanUtils.copyProperties(funnelRecDto,recruitPo);
//        Date date = handleDate(funnelRecDto.getRecReleaseDate());
//        recruitPo.setRecReleaseDate(date);
        return recruitPo;
    }

    //处理时间数据（未测试能否正常使用过）
    private Date handleDate(String sDate){
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        if ("不限".equals(sDate)){
            date = null;
        }else if("一天以内".equals(sDate)){
            calendar.add(calendar.DATE,-1); //把日期往后增加一天,整数  往后推,负数往前移动
            date=calendar.getTime(); //这个时间就是日期往后推一天的结果
        }else if("三天以内".equals(sDate)){
            calendar.add(calendar.DATE, -3);
            date = calendar.getTime();
        }else if("七天以内".equals(sDate)){
            calendar.add(calendar.DATE, -7);
            date = calendar.getTime();
        }else if("十五以内".equals(sDate)){
            calendar.add(calendar.DATE, -15);
            date = calendar.getTime();
        }else if("一个月以内".equals(sDate)){
            calendar.add(calendar.MONTH, -1);
            date = calendar.getTime();
        }else {
            date = null;
        }
        return date;
    }

    @Override
    public int update(RecruitPo recruitPo) {
        return recruitMapper.update(recruitPo);
    }

    @Override
    public int delete(Integer id) {
        return recruitMapper.delete(id);
    }
}
